/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-10-24
 * Time:15:06
 */
class ListNode{
    public int val;
    public ListNode next;
    public ListNode(int val){
        this.val = val;
    }
}
public class MyLinkedList {
    public ListNode head;

    //穷举法创建链表
    public void createLinkedList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        this.head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
    }

    //打印链表
    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void display2(ListNode node){
        ListNode cur = node;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
     public int getSize(){
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
     }

     public void clear(){
        this.head = null;
     }

     //头插法
     public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
     }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            return;
        }
        ListNode cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //找到下标节点的前一个结点
    private ListNode findIndexSubOne(int index){
        if(this.head == null){
            return null;
        }
        ListNode cur = this.head;
        while(index - 1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        if(index < 0 || index > this.getSize()){
            System.out.println("插入的位置不合法");
            return;
        }
        if(index == 0){
            this.addFirst(data);
            return;
        }
        if(index == this.getSize()){
            this.addLast(data);
            return;
        }
        ListNode cur = this.findIndexSubOne(index);
        node.next = cur.next;
        cur.next = node;

    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(this.head == null){
            return false;
        }
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    private ListNode searchPrev(int key){
        ListNode cur = this.head;
        while(cur.next != head){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }
        //首先找到key结点的前一个结点
        ListNode prev = this.searchPrev(key);
        if(prev == null){
            System.out.println("没有要删除的结点");
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head == null){
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
            }else{
                prev = cur;
            }
            cur = cur.next;
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }
    }

    //2. 反转一个单链表。
    public ListNode reverseList() {
        ListNode prev = null;
        ListNode cur = this.head;
        ListNode curNext = null;
        while(cur != null){
            curNext = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return prev;
    }
    // 3. 给定一个带有头结点 head 的非空单链表，返回链表的中间结点。如果有两个中间结点，则返回第二个中间结点。
    public ListNode middleNode() {
        ListNode slow = this.head;
        ListNode fast = this.head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    //4. 输入一个链表，输出该链表中倒数第k个结点。
    public ListNode FindKthToTail(int k) {
        //双指针法，保持两个指针相差k-1步
        //判断k的合法性
        //想让fast先走k-1步
        //之后fast slow 各走一步直到fast为最后一个节点
        if(k < 0 ){
            return null;
        }
        ListNode slow = this.head;
        ListNode fast = this.head;
        while(k - 1 != 0){
            fast = fast.next;
            if(fast == null){
                return null;
            }
            k--;
        }
        while(fast.next != null){
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    //5. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
    //前面测试类中已经写过
    // 6. 编写代码，以给定值x为基准将链表分割成两部分，所有小于x的结点排在大于或等于x的结点之前 。
    public ListNode partition( int x) {
        if (this.head ==  null) {
            return null;
        }
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val < x){
                if(bs == null){
                    bs = cur;
                    be = cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else{
                if(as == null){
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(be == null){
            return as;
        }
        be.next = as;
        if(as != null){
            ae.next = null;
        }
        return bs;
    }


    public ListNode reverseBetween( int left, int right) {
        if(this.head == null){
            return null;
        }

        // 因为头节点有可能发生变化，使用虚拟头节点可以避免复杂的分类讨论
        ListNode newNode = new ListNode(-1);
        newNode.next = this.head;
        ListNode pre = newNode;

        // 第 1 步：从虚拟头节点走 left - 1 步，来到 left 节点的前一个节点
        // 建议写在 for 循环里，语义清晰
        for(int i = 0; i  < left - 1;i++){
            pre = pre.next;
        }

        // 第 2 步：从 pre 再走 right - left + 1 步，来到 right 节点
        ListNode rightNode = pre;
        for(int i = 0 ;i < right - left + 1;i++){
            rightNode = rightNode.next;
        }

        // 第 3 步：切断出一个子链表（截取链表）
        ListNode leftNode = pre.next;
        ListNode curr = rightNode.next;
        pre.next = null;
        rightNode.next = null;

        // 第 4 步：同第 206 题，反转链表的子区间
        reverseLinkedList2(leftNode);

        // 第 5 步：接回到原来的链表中
        pre.next = rightNode;
        leftNode.next = curr;
        return newNode.next;

    }
    public void reverseLinkedList2(ListNode node){
        ListNode pre = null;
        ListNode cur = node;
        ListNode curNext = null;
        while(cur != null){
            curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
    }

}
